<!DOCTYPE html>
<html lang="en">
<head><!-- hexo injector head_begin start -->
        <link rel="preconnect" href="https://blog.fantasyke.cn/picx-images-hosting" crossorigin=""><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/uploads/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/uploads/avatar.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="0qBBGdG7vYORyT5XLZRUqQZ_y5kKYhgyUwNqjSIImg0">
  <meta name="msvalidate.01" content="63C90D0EC3DA6C31FD109132B7BEF502">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Zen+Maru+Gothic:300,300italic,400,400italic,700,700italic%7CZCOOL+XiaoWei:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.fantasyke.cn","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.21.1","exturl":true,"sidebar":{"position":"right","width_expanded":350,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":null},"fold":{"enable":true,"height":500},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":true,"nav":{"gitalk":{"text":"gitalk","order":-1}},"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="主从复制 简介 单机Redis的风险与问题">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis集群">
<meta property="og:url" content="https://blog.fantasyke.cn/posts/4509/index.html">
<meta property="og:site_name" content="Fantasy&#39;Ke Blog">
<meta property="og:description" content="主从复制 简介 单机Redis的风险与问题">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.esj6icd70.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.5c100cqaqf.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.51e677b88z.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.1hs8he8pup.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.45pdcxtoj.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.4xuk9hitls.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.64dvi37ves.webp">
<meta property="article:published_time" content="2023-06-01T07:28:00.000Z">
<meta property="article:modified_time" content="2024-12-17T06:36:35.399Z">
<meta property="article:author" content="Fantasy-ke">
<meta property="article:tag" content="Redis">
<meta property="article:tag" content="NOSQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fantasy-ke.github.io/picx-images-hosting/redis/image.esj6icd70.webp">


<link rel="canonical" href="https://blog.fantasyke.cn/posts/4509/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://blog.fantasyke.cn/posts/4509/","path":"posts/4509/","title":"Redis集群"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Redis集群 | Fantasy'Ke Blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Fantasy'Ke Blog" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Fantasy'Ke Blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="Search" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li><li class="menu-item menu-item-photos"><a href="/photos/" rel="section"><i class="fas fa-camera-retro fa-fw"></i>photos</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="Searching..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">1.</span> <span class="nav-text"> 主从复制</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-2"><span class="nav-number">1.1.</span> <span class="nav-text"> 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-3"><span class="nav-number">1.1.1.</span> <span class="nav-text"> 单机Redis的风险与问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-4"><span class="nav-number">1.1.2.</span> <span class="nav-text"> 解决方案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-5"><span class="nav-number">1.1.3.</span> <span class="nav-text"> 解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-6"><span class="nav-number">1.1.4.</span> <span class="nav-text"> 主从复制的作用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-7"><span class="nav-number">1.2.</span> <span class="nav-text"> 工作流程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-8"><span class="nav-number">1.2.1.</span> <span class="nav-text"> 建立连接阶段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-9"><span class="nav-number">1.2.2.</span> <span class="nav-text"> 连接阶段(slave连master)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-10"><span class="nav-number">1.2.3.</span> <span class="nav-text"> 授权阶段(master没密码可省略)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-11"><span class="nav-number">1.2.4.</span> <span class="nav-text"> 数据同步阶段</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#-12"><span class="nav-number">1.2.4.1.</span> <span class="nav-text"> 同步阶段master说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-13"><span class="nav-number">1.2.4.2.</span> <span class="nav-text"> 同步阶段slave说明</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-14"><span class="nav-number">1.2.5.</span> <span class="nav-text"> 命令传播阶段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-15"><span class="nav-number">1.2.6.</span> <span class="nav-text"> 命令传播阶段的部分复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-16"><span class="nav-number">1.2.7.</span> <span class="nav-text"> 数据同步+命令传播阶段工作流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-17"><span class="nav-number">1.2.8.</span> <span class="nav-text"> 心跳机制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#-18"><span class="nav-number">1.2.8.1.</span> <span class="nav-text"> master心跳任务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-19"><span class="nav-number">1.2.8.2.</span> <span class="nav-text"> slave心跳任务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-20"><span class="nav-number">1.2.8.3.</span> <span class="nav-text"> 心跳阶段注意事项</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-21"><span class="nav-number">1.3.</span> <span class="nav-text"> 主从复制的完整工作流程图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-22"><span class="nav-number">1.4.</span> <span class="nav-text"> 常见问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-23"><span class="nav-number">1.4.1.</span> <span class="nav-text"> 频繁的全量复制(1)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-24"><span class="nav-number">1.4.2.</span> <span class="nav-text"> 频繁的全量复制(2)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-25"><span class="nav-number">1.4.3.</span> <span class="nav-text"> 频繁的网络中断(1)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-26"><span class="nav-number">1.4.4.</span> <span class="nav-text"> 频繁的网络中断(2)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-27"><span class="nav-number">1.4.5.</span> <span class="nav-text"> 数据不一致</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-28"><span class="nav-number">2.</span> <span class="nav-text"> 哨兵模式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-29"><span class="nav-number">2.1.</span> <span class="nav-text"> 哨兵</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-30"><span class="nav-number">2.2.</span> <span class="nav-text"> 启动哨兵</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-31"><span class="nav-number">2.3.</span> <span class="nav-text"> sentinel.conf</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-32"><span class="nav-number">2.4.</span> <span class="nav-text"> 工作原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-33"><span class="nav-number">2.4.1.</span> <span class="nav-text"> 监控</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-34"><span class="nav-number">2.4.2.</span> <span class="nav-text"> 通知</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-35"><span class="nav-number">2.4.3.</span> <span class="nav-text"> 故障转移</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-36"><span class="nav-number">3.</span> <span class="nav-text"> 集群</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-37"><span class="nav-number">3.1.</span> <span class="nav-text"> 现状问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-38"><span class="nav-number">3.2.</span> <span class="nav-text"> 集群架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-39"><span class="nav-number">3.3.</span> <span class="nav-text"> 集群的数据存储</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-40"><span class="nav-number">3.4.</span> <span class="nav-text"> 集群的内部通讯设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-41"><span class="nav-number">3.5.</span> <span class="nav-text"> 集群搭建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-42"><span class="nav-number">3.6.</span> <span class="nav-text"> Cluster节点操作命令</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Fantasy-ke"
      src="/uploads/avatar.png">
  <p class="site-author-name" itemprop="name">Fantasy-ke</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;fantasy-ke"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly94LmNvbS9BX0RyZWFtZXJfSGFyZA==" title="Twitter → https:&#x2F;&#x2F;x.com&#x2F;A_Dreamer_Hard"><i class="fab fa-twitter fa-fw"></i>Twitter</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vQCVFNSU5MSVBOCVFNiVBMiVBNi16OWk=" title="YouTube → https:&#x2F;&#x2F;www.youtube.com&#x2F;@%E5%91%A8%E6%A2%A6-z9i"><i class="fab fa-youtube fa-fw"></i>YouTube</span>
      </span>
  </div>
<div class="cc-license animated" itemprop="sponsor">
  <a href="https://www.netlify.com" class="cc-opacity" title="Deploy with Netlify → https://www.netlify.com" target="_blank"><img width="80" src="https://www.netlify.com/img/global/badges/netlify-dark.svg" alt="Netlify"></a>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css">
<div id="aplayer" style="position:relative;left;0;bottom:0;"></div>
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
<script>
  const ap = new APlayer({
    container: document.getElementById('aplayer'),
    autoplay: false, //自动播放
    listFolded: true, //播放列表默认折叠
    listMaxHeight: 90, //播放列表最大高度
    order: 'list', //音频循环顺序, 可选值: 'list', 'random'
    loop: 'all', //音频循环播放, 可选值: 'all', 'one', 'none'
    theme: '#e9e9e9', //切换音频时的主题色，优先级低于audio.theme
    preload: 'none', //音频预加载，可选值: 'none', 'metadata', 'auto'
    mutex: true, //互斥，阻止多个播放器同时播放，当前播放器播放时暂停其他播放器
    lrcType: 3, //歌词格式，可选值：3（LRC文件歌词格式），1（JS字符串歌词格式）
    volume: 0.7, //默认音量，请注意播放器会记忆用户设置，用户手动设置音量后默认音量即失效
    fixed: false, //吸底模式（fixed:true），迷你模式（mini:true），普通模式（注释此行或者设置fixed:false）
    audio: [{
        name: '平凡之路',
        artist: '朴树',
        lrc: '/downloads/lrc/平凡之路-朴树.lrc',
        cover: 'http://p2.music.126.net/W_5XiCv3rGS1-J7EXpHSCQ==/18885211718782327.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C4000007nRJ74eQsF6.m4a?guid=942726817&vkey=64755357C5716983BEECE2981FEC3954F0DD03E7630EFF769D9C40C31F50D53E7D260CB63E22CE8F954B1144083716A1BABFC2093F8F1C14&uin=&fromtag=120032'
      },
      {
        name: '后会无期',
        artist: '徐良,汪苏泷',
        lrc: '/downloads/lrc/后会无期-G.E.M.邓紫棋.lrc',
        cover: 'http://p1.music.126.net/vpvPajo3kn88nHc7jUjeWQ==/5974746185758035.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C400004ECnYg0QNchK.m4a?guid=893251117&vkey=D9DCBB8C4A4CAF8550E97A2A574DE1231DFDD8BC0989CC724742C7B9831C4108E0A805A46E0F2B2A556228467C146B9C312E3E8B498614C7&uin=&fromtag=120032'
      }
    ]
  });

  // 实现切换音频时，根据音频的封面图片自适应主题色
  const colorThief = new ColorThief();
  const setTheme = (index) => {
    if (!ap.list.audios[index].theme) {
      colorThief.getColorAsync(ap.list.audios[index].cover, function(color) {
        ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
      });
    }
  };
  setTheme(ap.list.index);
  ap.on('listswitch', (data) => {
    setTheme(data.index);
  });
</script>



        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          Links
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">SunBlog</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">做梦的努力者</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0ZhbnRhc3ktS2U=" title="https:&#x2F;&#x2F;github.com&#x2F;Fantasy-Ke">github</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20vZmFudGFzeV9rZQ==" title="https:&#x2F;&#x2F;gitee.com&#x2F;fantasy_ke">gitee</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vZmFudGFzeS1rZQ==" title="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;fantasy-ke">我的园子</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8v" title="https:&#x2F;&#x2F;abp.io&#x2F;">abp</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8vZG9jcy9sYXRlc3Qv" title="https:&#x2F;&#x2F;abp.io&#x2F;docs&#x2F;latest&#x2F;">abp doc</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9ibG9ncy5tYXNhc3RhY2suY29tLw==" title="https:&#x2F;&#x2F;blogs.masastack.com&#x2F;">masa blog</span>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://blog.fantasyke.cn/posts/4509/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.png">
      <meta itemprop="name" content="Fantasy-ke">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Redis集群 | Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis集群
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2023-06-01 15:28:00" itemprop="dateCreated datePublished" datetime="2023-06-01T15:28:00+08:00">2023-06-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2024-12-17 14:36:35" itemprop="dateModified" datetime="2024-12-17T14:36:35+08:00">2024-12-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB%E7%9A%84%E7%A0%96/" itemprop="url" rel="index"><span itemprop="name">杂七杂八的砖</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="Word count in article">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">Word count in article: </span>
      <span>5.3k</span>
    </span>
    <span class="post-meta-item" title="Reading time">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">Reading time &asymp;</span>
      <span>19 mins.</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><!-- more -->
<!-- - [主从复制](#主从复制)
  - [简介](#简介)
    - [单机Redis的风险与问题](#单机redis的风险与问题)
    - [解决方案](#解决方案)
    - [解析](#解析)
    - [主从复制的作用](#主从复制的作用)
  - [工作流程](#工作流程)
    - [建立连接阶段](#建立连接阶段)
    - [连接阶段(slave连master)](#连接阶段slave连master)
    - [授权阶段(master没密码可省略)](#授权阶段master没密码可省略)
    - [数据同步阶段](#数据同步阶段)
      - [同步阶段master说明](#同步阶段master说明)
      - [同步阶段slave说明](#同步阶段slave说明)
    - [命令传播阶段](#命令传播阶段)
    - [命令传播阶段的部分复制](#命令传播阶段的部分复制)
    - [数据同步+命令传播阶段工作流程](#数据同步命令传播阶段工作流程)
    - [心跳机制](#心跳机制)
      - [master心跳任务](#master心跳任务)
      - [心跳阶段注意事项](#心跳阶段注意事项)
  - [主从复制的完整工作流程图](#主从复制的完整工作流程图)
  - [常见问题](#常见问题)
    - [频繁的全量复制(1)](#频繁的全量复制1)
    - [频繁的全量复制(2)](#频繁的全量复制2)
    - [频繁的网络中断(1)](#频繁的网络中断1)
    - [频繁的网络中断(2)](#频繁的网络中断2)
    - [数据不一致](#数据不一致)
- [哨兵模式](#哨兵模式)
  - [哨兵](#哨兵)
  - [启动哨兵](#启动哨兵)
  - [sentinel.conf](#sentinelconf)
  - [工作原理](#工作原理)
    - [监控](#监控)
    - [通知](#通知)
    - [故障转移](#故障转移)
- [集群](#集群)
  - [现状问题](#现状问题)
  - [集群架构](#集群架构)
  - [集群的数据存储](#集群的数据存储)
  - [集群的内部通讯设计](#集群的内部通讯设计)
  - [集群搭建](#集群搭建)
  - [Cluster节点操作命令](#cluster节点操作命令) -->
<h1 id=""><a class="markdownIt-Anchor" href="#"></a> <a href="#%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6" title="主从复制"></a>主从复制</h1>
<h2 id="-2"><a class="markdownIt-Anchor" href="#-2"></a> <a href="#%E7%AE%80%E4%BB%8B" title="简介"></a>简介</h2>
<p>互联网”三高”架构：高并发、高性能、高可用</p>
<p>_高可用：业界可用性目标5个9，即*_99.999%**，即全年服务器宕机时长低于315秒，**约5.25分钟***</p>
<h3 id="-3"><a class="markdownIt-Anchor" href="#-3"></a> <a href="#%E5%8D%95%E6%9C%BARedis%E7%9A%84%E9%A3%8E%E9%99%A9%E4%B8%8E%E9%97%AE%E9%A2%98" title="单机Redis的风险与问题"></a>单机Redis的风险与问题</h3>
<ul>
<li>问题1：机器故障
<ul>
<li>现象：硬盘故障、系统崩溃</li>
<li>本质：数据丢失，很可能对业务造成灾难性打击</li>
<li>结论：基本上会放弃使用Redis</li>
</ul>
</li>
<li>问题2：容量瓶颈
<ul>
<li>现象：内存不足，从16G升级到64G，从64G升级到128G，无限升级内存</li>
<li>本质：穷！硬件条件跟不上</li>
<li>结论：放弃使用Redis</li>
</ul>
</li>
<li>结论：为了避免单点Redis服务器故障，准备多台服务器，互相连通，将数据复制从多个副本保存在不同的服务器上，<strong>连接在一起</strong>，并保证数据是<strong>同步</strong>的，即使有其中一台服务器宕机，其他服务器依然可以继续提供服务，实现Redis的高可用，同时实现数据<code>冗余备份</code></li>
</ul>
<h3 id="-4"><a class="markdownIt-Anchor" href="#-4"></a> <a href="#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88" title="解决方案"></a>解决方案</h3>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.esj6icd70.webp" alt="Redis主从复制.png" /></p>
<ul>
<li>提供数据方：master
<ul>
<li>主服务器、主节点、主库</li>
<li>主客户端</li>
</ul>
</li>
<li>接受数据方：slave
<ul>
<li>从服务器、从节点、从库</li>
<li>从客户端</li>
</ul>
</li>
<li>需要解决的问题：数据同步</li>
<li>核心工作：master的数据复制到slave中</li>
</ul>
<h3 id="-5"><a class="markdownIt-Anchor" href="#-5"></a> <a href="#%E8%A7%A3%E6%9E%90" title="解析"></a>解析</h3>
<p>主从复制即将master中的数据即时、有效的复制到slave中</p>
<p>特征：一个master可以拥有多个slave，一个slave只对应一个master</p>
<p>职责：</p>
<ul>
<li>master：
<ul>
<li>写数据</li>
<li>执行写操作时，将出现变化的数据自动同步到slave</li>
<li>读数据（可忽略）</li>
</ul>
</li>
<li>slave
<ul>
<li>读数据</li>
<li>写数据（禁止）</li>
</ul>
</li>
</ul>
<h3 id="-6"><a class="markdownIt-Anchor" href="#-6"></a> <a href="#%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E7%9A%84%E4%BD%9C%E7%94%A8" title="主从复制的作用"></a>主从复制的作用</h3>
<ul>
<li>读写分离：master写、slave读，提高服务器的读写负载能力</li>
<li>负载均衡：基于主从结构，配合读写分离，由slave分担master负载，并根据需求的变化，改变slave的数量，通过多个从节点分担数据读取负载，大大提高Redis服务器并发量与数据吞吐量</li>
<li>故障恢复：当master出现问题时，由slave提供服务，实现快速的故障恢复</li>
<li>数据冗余：实现数据热备份，是持久化之外的一种数据冗余方式</li>
<li>高可用基石：基于主从复制，构建哨兵与集群，实现Redis的高可用方案</li>
</ul>
<h2 id="-7"><a class="markdownIt-Anchor" href="#-7"></a> <a href="#%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B" title="工作流程"></a>工作流程</h2>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.5c100cqaqf.webp" alt="Redis主从复制流程.png" /></p>
<ul>
<li>主从复制过程大体可以分为3个阶段
<ul>
<li>建立连接阶段（即准备阶段）</li>
<li>数据同步阶段</li>
<li>命令传播阶段</li>
</ul>
</li>
</ul>
<h3 id="-8"><a class="markdownIt-Anchor" href="#-8"></a> <a href="#%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5%E9%98%B6%E6%AE%B5" title="建立连接阶段"></a>建立连接阶段</h3>
<p>建立slave到master的连接，使master能够识别slave，并保存slave端口号<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.51e677b88z.webp" alt="Redis主从复制流程2.png" /></p>
<ol>
<li>[slave]设置master的地址和端口，保存master信息</li>
<li>[slave]建立socket连接</li>
<li>[slave]发送ping命令(定时器任务)</li>
<li>[slave]身份验证</li>
<li>[slave]发送slave端口信息</li>
<li>至此主从连接成功</li>
<li>达到了以下状态：
<ol>
<li>slave：保存master的地址与端口</li>
<li>master：保存slave的端口</li>
<li>总体：master与slave之间创建了连接的socket</li>
</ol>
</li>
</ol>
<h3 id="-9"><a class="markdownIt-Anchor" href="#-9"></a> <a href="#%E8%BF%9E%E6%8E%A5%E9%98%B6%E6%AE%B5-slave%E8%BF%9Emaster" title="连接阶段(slave连master)"></a>连接阶段(slave连master)</h3>
<ul>
<li>
<p>方式一：客户端发送命令：<code>slaveof &lt;masterip&gt; &lt;masterport&gt;</code></p>
</li>
<li>
<p>方式二：启动服务器参数：<code>redis-server -slaveof &lt;masterip&gt; &lt;masterport&gt;</code></p>
</li>
<li>
<p>方式三：服务器配置：<code>slaveof &lt;masterip&gt; &lt;masterport&gt;</code></p>
</li>
<li>
<p>slave系统信息</p>
<ul>
<li><code>master_link_down_since_seconds</code></li>
<li><code>masterhost</code></li>
<li><code>masterport</code></li>
</ul>
</li>
<li>
<p>master系统信息</p>
<ul>
<li><code>slave_listening_port</code>（多个）</li>
</ul>
</li>
</ul>
<h3 id="-10"><a class="markdownIt-Anchor" href="#-10"></a> <a href="#%E6%8E%88%E6%9D%83%E9%98%B6%E6%AE%B5-master%E6%B2%A1%E5%AF%86%E7%A0%81%E5%8F%AF%E7%9C%81%E7%95%A5" title="授权阶段(master没密码可省略)"></a>授权阶段(master没密码可省略)</h3>
<ul>
<li>
<p>master配置文件设置密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">requirepass &lt;password&gt;  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>master客户端发送命令设置密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">config get requirepass &lt;password&gt;  </span><br><span class="line">config get requirepass  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>slave客户端发送命令设置密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">auth &lt;password&gt;  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>slave配置文件设置密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">masterauth &lt;password&gt;  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>启动客户端设置密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -a &lt;password&gt;  </span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="-11"><a class="markdownIt-Anchor" href="#-11"></a> <a href="#%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5%E9%98%B6%E6%AE%B5" title="数据同步阶段"></a>数据同步阶段</h3>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.1hs8he8pup.webp" alt="Redis主从复制数据同步阶段.png" /></p>
<ol>
<li>[slave]请求同步数据</li>
<li>[master]创建RDB同步数据</li>
<li>[slave]恢复RDB同步数据</li>
<li>[slave]请求部分同步数据</li>
<li>[slave]恢复部分同步数据</li>
<li>至此数据同步工作完成</li>
<li>达到了以下状态
<ol>
<li>slave：具有master端全部数据，包含RDB过程接受的数据</li>
<li>master：保存slave当前数据同步的位置</li>
<li>总体：master与slave之间完成了数据克隆</li>
</ol>
</li>
</ol>
<h4 id="-12"><a class="markdownIt-Anchor" href="#-12"></a> <a href="#%E5%90%8C%E6%AD%A5%E9%98%B6%E6%AE%B5master%E8%AF%B4%E6%98%8E" title="同步阶段master说明"></a>同步阶段master说明</h4>
<ol>
<li>
<p>如果master数据量巨大，数据同步阶段应避免流量高峰期，避免造成master阻塞，影响业务正常执行</p>
</li>
<li>
<p>复制缓冲区大小设定不合理，会导致数据溢出，如进行全量复制周期太长，进行部分复制时发现数据已经存在丢失的情况，必须进行第二次全量复制，致使slave陷入死循环状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">repl-backlog-size 1mb  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>master单机内存占用主机内存的比例不应过大，建议使用50%70%的内存，留下30%50%的内存用于执行bgsave命令和创建复制缓冲区</p>
</li>
</ol>
<h4 id="-13"><a class="markdownIt-Anchor" href="#-13"></a> <a href="#%E5%90%8C%E6%AD%A5%E9%98%B6%E6%AE%B5slave%E8%AF%B4%E6%98%8E" title="同步阶段slave说明"></a>同步阶段slave说明</h4>
<ol>
<li>
<p>为避免slave进行全量复制、部分复制时服务器响应阻塞或数据不同步，建议关闭此期间的对外服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">slave-serve-stable-data <span class="built_in">yes</span>|no  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>数据同步阶段，master发送给slave信息可以理解master是slave的一个客户端，主动向slave发送命令</p>
</li>
<li>
<p>多个slave同时向master请求数据同步，master发送的RDB文件增多，如果master带宽不足，会对带宽造成巨大冲击，因此数据同步需要根据业务需求，适量错峰</p>
</li>
<li>
<p>slave过多时，建议调整拓扑结构，由一主多从从结构变成树状结构，中间的节点既是master，也是slave。注意使用树状结构时，由于层级深度，导致深度越高的slave与顶层master间数据同步延迟较大，数据一致性变差，应谨慎选择</p>
</li>
</ol>
<h3 id="-14"><a class="markdownIt-Anchor" href="#-14"></a> <a href="#%E5%91%BD%E4%BB%A4%E4%BC%A0%E6%92%AD%E9%98%B6%E6%AE%B5" title="命令传播阶段"></a>命令传播阶段</h3>
<ul>
<li>当master数据库状态被修改时，导致主从服务器数据库状态不一致，此时需要让主从数据同步到一致的状态，同步的动作称为命令传播</li>
<li>master将接收到的数据变更命令发送给slave，slave接受命令后执行命令</li>
</ul>
<h3 id="-15"><a class="markdownIt-Anchor" href="#-15"></a> <a href="#%E5%91%BD%E4%BB%A4%E4%BC%A0%E6%92%AD%E9%98%B6%E6%AE%B5%E7%9A%84%E9%83%A8%E5%88%86%E5%A4%8D%E5%88%B6" title="命令传播阶段的部分复制"></a>命令传播阶段的部分复制</h3>
<ul>
<li>
<p>命令传播阶段出现断网现象：</p>
<ul>
<li>网络闪断闪联 –&gt; 忽略</li>
<li>短时间网络中断 –&gt; 部分复制</li>
<li>长时间网络中断 –&gt; 全量复制</li>
</ul>
</li>
<li>
<p>部分复制的三个核心要素</p>
<ul>
<li>服务器的运行id(runid)
<ul>
<li>概念：服务器运行Id，是每一台服务器每次运行的身份识别码，一台服务器多次运行可以生成多个运行id</li>
<li>组成：运行id由40位字符组成，是一个随机的十六进制字符</li>
<li>作用：运行id被用于在服务器间进行传输，识别身份。如果想两次操作均同一台服务器进行，必须每次操作携带对应的运行id，用于对方识别</li>
<li>实现方式：运行id在每台服务器启动时自动生成的，master在首次连接slave时，会将自己的运行id发送给slave，slave保存此id，通过<code>info server</code>命令，可以查看节点的runid</li>
</ul>
</li>
<li>主服务器的复制积压缓冲区
<ul>
<li>概念：复制缓冲区，又名复制积压缓冲区，是一个先进先出(FIFO)的队列，用于存储服务器执行过的命令，每次传播命令，master都会将传播的命令记录下来，并存储在复制缓冲区
<ul>
<li>复制缓冲区默认数据存储空间大小是1M，由于存储空间大小是固定的，当入队元素的数量大于队列长度时，最先入队的元素会被弹出，而新元素会被放入队列</li>
</ul>
</li>
<li>由来：每台服务器启动时，如果开启有AOF或被连接成位master节点，即创建复制缓冲区</li>
<li>作用：用于保存master收到的所有指令（仅影响数据变更的指令，例如set、select）</li>
<li>数据来源：当master接收到主客户端的指令时，除了将指令执行，还会将指令存储到缓冲区中</li>
<li>组成：偏移量和字节值</li>
<li>工作原理
<ul>
<li>通过offset区分不同的slave当前数据传播的差异</li>
<li>master记录已发送的信息对应的offset</li>
<li>slave记录已接受的信息对应的offset</li>
</ul>
</li>
</ul>
</li>
<li>主从服务器的复制偏移量
<ul>
<li>概念：一个数字，描述复制缓冲区中的指令字节位置</li>
<li>分类：
<ul>
<li>master复制偏移量：记录发送给所有slave的指令字节对应的位置（多个）</li>
<li>slave复制偏移量：记录slave接受master发送过来的指令字节对应的位置（一个）</li>
</ul>
</li>
<li>数据来源：
<ul>
<li>master端：发送一次记录一次</li>
<li>slave端：接受一次记录一次</li>
</ul>
</li>
<li>作用：同步信息，比对master与slave的差异，当slave断线后，恢复数据使用</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="-16"><a class="markdownIt-Anchor" href="#-16"></a> <a href="#%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5-%E5%91%BD%E4%BB%A4%E4%BC%A0%E6%92%AD%E9%98%B6%E6%AE%B5%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B" title="数据同步+命令传播阶段工作流程"></a>数据同步+命令传播阶段工作流程</h3>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.45pdcxtoj.webp" alt="Redis数据同步完整流程.png" /></p>
<h3 id="-17"><a class="markdownIt-Anchor" href="#-17"></a> <a href="#%E5%BF%83%E8%B7%B3%E6%9C%BA%E5%88%B6" title="心跳机制"></a>心跳机制</h3>
<p>进入命令传播阶段，master与slave间需要进行信息交换，使用心跳机制进行维护，实现双方连接保持在线</p>
<h4 id="-18"><a class="markdownIt-Anchor" href="#-18"></a> <a href="#master%E5%BF%83%E8%B7%B3%E4%BB%BB%E5%8A%A1" title="master心跳任务"></a>master心跳任务</h4>
<ul>
<li>
<p>指令：<code>PING</code></p>
</li>
<li>
<p>周期：由<code>repl-ping-slave-period</code>决定，默认10秒</p>
</li>
<li>
<p>作用：判断slave是否在线</p>
</li>
<li>
<p>查询：<code>info replication</code> 获取slave最后一次连接时间间隔，lag项维持在0或1视为正常</p>
<h4 id="-19"><a class="markdownIt-Anchor" href="#-19"></a> <a href="#slave%E5%BF%83%E8%B7%B3%E4%BB%BB%E5%8A%A1" title="slave心跳任务"></a>slave心跳任务</h4>
</li>
<li>
<p>指令：<code>REPLCONF ACK &#123;offset&#125;</code></p>
</li>
<li>
<p>周期：1秒</p>
</li>
<li>
<p>作用1：汇报slave自己的复制偏移量，获取最新的数据变更指令</p>
</li>
<li>
<p>作用2：判断master是否在线</p>
</li>
</ul>
<h4 id="-20"><a class="markdownIt-Anchor" href="#-20"></a> <a href="#%E5%BF%83%E8%B7%B3%E9%98%B6%E6%AE%B5%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9" title="心跳阶段注意事项"></a>心跳阶段注意事项</h4>
<ul>
<li>
<p>当slave多数掉线，或延迟过高时，master为保障数据稳定性，将拒绝所有信息同步操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">min-slave-to-write 2  </span><br><span class="line">min-slave-max-lag 8  </span><br></pre></td></tr></table></figure>
<p>slave数量少于2个，或者所有slave的延迟都大于等于10秒时，强制关闭master写功能，停止数据同步</p>
</li>
<li>
<p>slave数量由slave发送<code>REPLCONF ACK</code>命令做确认</p>
</li>
<li>
<p>slave延迟由slave发送<code>REPLCONF ACK</code>命令做确认</p>
</li>
</ul>
<h2 id="-21"><a class="markdownIt-Anchor" href="#-21"></a> <a href="#%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E7%9A%84%E5%AE%8C%E6%95%B4%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B%E5%9B%BE" title="主从复制的完整工作流程图"></a>主从复制的完整工作流程图</h2>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.4xuk9hitls.webp" alt="主从复制的完整工作流程.png" /></p>
<h2 id="-22"><a class="markdownIt-Anchor" href="#-22"></a> <a href="#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98" title="常见问题"></a>常见问题</h2>
<h3 id="-23"><a class="markdownIt-Anchor" href="#-23"></a> <a href="#%E9%A2%91%E7%B9%81%E7%9A%84%E5%85%A8%E9%87%8F%E5%A4%8D%E5%88%B6-1" title="频繁的全量复制(1)"></a>频繁的全量复制(1)</h3>
<p>伴随着系统的运行，master的数据量会越来越大，一旦master重启，runid将发生变化，会导致全部slave的全量复制操作</p>
<ul>
<li>内部优化调整方案（redis自己操作的）
<ul>
<li>master内部创建master_replid变量，使用runid相同的策略生成，长度41位，并发送给所有slave</li>
<li>在master关闭时执行命令<code>shutdown save</code>，进行RDB持久化，将runid与offset保存到RDB文件中
<ul>
<li>repl-id repl-offset</li>
<li>通过redis-check-rdb命令可以查看该信息</li>
</ul>
</li>
<li>master重启后加载RDB文件，恢复数据，重启后RDB文件将保存的repl-id与repl-offset加载到内存中
<ul>
<li>master_repl_id = repl master_repl_offset = repl-offset</li>
<li>通过info命令可以查看该信息</li>
</ul>
</li>
</ul>
</li>
<li>作用：本机保存上次runid，重启后恢复该值，使所有slave认为还是之前的master</li>
</ul>
<h3 id="-24"><a class="markdownIt-Anchor" href="#-24"></a> <a href="#%E9%A2%91%E7%B9%81%E7%9A%84%E5%85%A8%E9%87%8F%E5%A4%8D%E5%88%B6-2" title="频繁的全量复制(2)"></a>频繁的全量复制(2)</h3>
<ul>
<li>问题现象：网络环境不佳，出现网络中断，slave不提供服务</li>
<li>问题原因：复制缓冲区过小，断网后slave的offset越界，触发全量复制</li>
<li>最终结果：slave反复进行全量复制</li>
<li>解决方案：修复复制缓冲区大小</li>
<li>建议设置如下：
<ul>
<li>测算从master到slave的重连平均时长second</li>
<li>获取master平均每秒产生写命令数据总量<code>write_size_per_second</code></li>
<li>最有复制缓冲区空间 = 2 * second * write_size_per_second</li>
</ul>
</li>
</ul>
<h3 id="-25"><a class="markdownIt-Anchor" href="#-25"></a> <a href="#%E9%A2%91%E7%B9%81%E7%9A%84%E7%BD%91%E7%BB%9C%E4%B8%AD%E6%96%AD-1" title="频繁的网络中断(1)"></a>频繁的网络中断(1)</h3>
<ul>
<li>
<p>问题现象：master的CPU占用过高或slave频繁断开联系</p>
</li>
<li>
<p>问题原因：</p>
<ul>
<li>slave每1秒发送<code>REPLCONF ACK</code>命令到<code>master</code></li>
<li>当slave接到了慢查询时（<code>keys *</code>、<code>hgetall</code>等），会大量占用CPU性能</li>
<li>master每1秒调用复制定时函数<code>replicationCron()</code>，会对slave发现长时间没有进行响应</li>
</ul>
</li>
<li>
<p>最终结果：</p>
<ul>
<li>master各种资源（输出缓冲区、带宽、连接等）被严重占用</li>
</ul>
</li>
<li>
<p>解决方案：通过设置合理的超时时间，确认是否释放slave</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">repl-timeout  </span><br></pre></td></tr></table></figure>
<p>该参数定义了超时时间的阙值（默认60秒），超过该值，释放slave</p>
</li>
</ul>
<h3 id="-26"><a class="markdownIt-Anchor" href="#-26"></a> <a href="#%E9%A2%91%E7%B9%81%E7%9A%84%E7%BD%91%E7%BB%9C%E4%B8%AD%E6%96%AD-2" title="频繁的网络中断(2)"></a>频繁的网络中断(2)</h3>
<ul>
<li>
<p>问题现象：slave与master连接断开</p>
</li>
<li>
<p>问题原因：</p>
<ul>
<li>master发送<code>ping</code>指令频度较低</li>
<li>master设定超时时间较短</li>
<li>ping指令在网络中存在丢包</li>
</ul>
</li>
<li>
<p>解决方案：提高<code>ping</code>指令发送的频度</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">repl-ping-slave-period  </span><br></pre></td></tr></table></figure>
<p>超时时间<code>repl-time</code>的时间至少是<code>ping</code>指令频度的5到10倍，否则slave很容易判定超时</p>
</li>
</ul>
<h3 id="-27"><a class="markdownIt-Anchor" href="#-27"></a> <a href="#%E6%95%B0%E6%8D%AE%E4%B8%8D%E4%B8%80%E8%87%B4" title="数据不一致"></a>数据不一致</h3>
<ul>
<li>问题现象：多个slave获取相同数据不同步</li>
<li>问题原因：网络信息不同步，数据发送有延迟</li>
<li>解决方案：
<ul>
<li>
<p>优化主从间的网络环境，通常放置在同一个机房部署，如使用阿里云等云服务器要注意此现象</p>
</li>
<li>
<p>监控主从节点延迟（通过offset）判断，如果slave延迟过大，暂时屏蔽程序对该slave的数据访问</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">slave-serve-stable-data <span class="built_in">yes</span>|no  </span><br></pre></td></tr></table></figure>
<p>开启后仅响应info、slaveof等少数命令（慎用，除非对数据一致性要求很高）</p>
</li>
</ul>
</li>
</ul>
<h1 id="-28"><a class="markdownIt-Anchor" href="#-28"></a> <a href="#%E5%93%A8%E5%85%B5%E6%A8%A1%E5%BC%8F" title="哨兵模式"></a>哨兵模式</h1>
<p>主从复制模式中，如果master宕机了，需要做以下操作：</p>
<ol>
<li>将宕机的master下线</li>
<li>找一个slave作为master</li>
<li>通知所有的slave连接新的master</li>
<li>启动新的master与slave</li>
</ol>
<h2 id="-29"><a class="markdownIt-Anchor" href="#-29"></a> <a href="#%E5%93%A8%E5%85%B5" title="哨兵"></a>哨兵</h2>
<p>哨兵是一个分布式系统，用于对主从结构中的每台服务器进行监控，当出现故障时通过投票机制选择新的master并将所有slave连接到新的master</p>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/redis/image.64dvi37ves.webp" alt="image.png" /></p>
<p>哨兵的作用：</p>
<ul>
<li>监控
<ul>
<li>不断的检查master和slave是否正常运行</li>
<li>master存活检测、master与slave运行情况检测</li>
</ul>
</li>
<li>通知（提醒）：当被监控的服务器出现问题后，向其他（哨兵间、客户端）发送通知</li>
<li>自动故障转移：断开master与slave连接，选取一个slave作为master，将其他slave连接到新的master，并告知客户端新的服务器地址</li>
</ul>
<p><strong>哨兵也是一台redis服务器，只是不提供数据服务，通常哨兵配置数量为单数</strong></p>
<h2 id="-30"><a class="markdownIt-Anchor" href="#-30"></a> <a href="#%E5%90%AF%E5%8A%A8%E5%93%A8%E5%85%B5" title="启动哨兵"></a>启动哨兵</h2>
<ul>
<li>
<p>配置一拖二的主从结构</p>
</li>
<li>
<p>配置三个哨兵（配置相同，端口不同）</p>
<ul>
<li>参看<code>sentinel.conf</code></li>
</ul>
</li>
<li>
<p>启动哨兵</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-sentinel sentinel-端口号.conf  </span><br></pre></td></tr></table></figure>
</li>
<li>
<p>启动顺序</p>
<ol>
<li>启动master</li>
<li>启动slave</li>
<li>启动哨兵</li>
</ol>
</li>
</ul>
<h2 id="-31"><a class="markdownIt-Anchor" href="#-31"></a> <a href="#sentinel-conf" title="sentinel.conf"></a>sentinel.conf</h2>
<ul>
<li>
<p><code>port 26379</code>：哨兵对外的端口</p>
</li>
<li>
<p><code>dir /tmp</code>：哨兵的工作信息存储位置</p>
</li>
<li>
<p><code>sentinel monitor mymaster 127.0.0.1 6379 2</code></p>
<blockquote>
<p>设置哨兵监控的Master，其中mymaster是自己给master起的名字，可以自定义，后面使用的时候保持一致即可，最后面的2表示有多少个哨兵认为master挂了，就认定为挂了，一般设置(哨兵数量/2+1)</p>
</blockquote>
</li>
<li>
<p><code>sentinel down-after-milliseconds mymaster 30000</code></p>
<blockquote>
<p>master连接多长时间没响应就认为挂了</p>
</blockquote>
</li>
<li>
<p><code>sentinel parallel-syncs mymaster 1</code></p>
<blockquote>
<p>进行新的master切换的时候，一次有多少个slave来进行同步数据，这个值越小，对服务器性能压力越小，速度越慢，相反，这个值越大，对服务器性能压力就越大，与之对应的速度就越快</p>
</blockquote>
</li>
<li>
<p><code>sentinel failover-timeout mymaster 1800000</code></p>
<blockquote>
<p>在进行同步的时候，超过多长的时间算超时</p>
</blockquote>
</li>
</ul>
<h2 id="-32"><a class="markdownIt-Anchor" href="#-32"></a> <a href="#%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86" title="工作原理"></a>工作原理</h2>
<h3 id="-33"><a class="markdownIt-Anchor" href="#-33"></a> <a href="#%E7%9B%91%E6%8E%A7" title="监控"></a>监控</h3>
<p>用于同步各个节点的状态信息</p>
<ul>
<li>获取各个sentinel的状态（是否在线）</li>
<li>获取master的状态
<ul>
<li>master属性
<ul>
<li>runid</li>
<li>role：master</li>
</ul>
</li>
<li>各个slave的详细信息
<ul>
<li>runid</li>
<li>role：slave</li>
<li>master_host、master_port</li>
<li>offset</li>
<li>…</li>
</ul>
</li>
</ul>
</li>
</ul>
<ol>
<li>sentinelA在启动的时候，会先连接master，建立CMD连接，获取master信息，并在master的配置里新增自己的信息</li>
<li>sentinelA在获取到master信息后，通过master信息得到master的slave信息，然后连接slave，获取slave信息</li>
<li>sentinelB启动时连接master，获取master信息，这时候发现master已经有sentinelA连接过的记录，便与sentinelA建立起一条pub/sub通道(发布订阅通道)，再连接master对应的slave</li>
<li>再有其他的sentinel启动连到master，一样执行的是sentinelB的步骤，就这样，每个sentinel都与其他的sentinel建立连接，形成一个小型组网</li>
</ol>
<h3 id="-34"><a class="markdownIt-Anchor" href="#-34"></a> <a href="#%E9%80%9A%E7%9F%A5" title="通知"></a>通知</h3>
<p>多个sentinel中的其中一个向master和slave发送一条hello信息，确定是否在线，并将这个是否在线的结果发布到sentinel自己的组网里，通知其他sentinel这个结果</p>
<h3 id="-35"><a class="markdownIt-Anchor" href="#-35"></a> <a href="#%E6%95%85%E9%9A%9C%E8%BD%AC%E7%A7%BB" title="故障转移"></a>故障转移</h3>
<ol>
<li>当通知阶段中有一个sentinelA发送了hello信息给master，但是master没做反应，这时候这个sentinelA就认为这个master出现故障，便将这个master的状态标记为<code>sdown</code>(主观下线)，并将这个消息发布到sentinel的pub/sub的通道中，通知其他sentinel</li>
<li>其他sentinel街道sentinelA发布的消息，作为吃瓜群众赶紧也去发送hello信息给master，看看是不是真挂了。这时如果达到了<code>sentinel.conf</code>里配置的数量的sentinel认为master挂了，那所有的sentinel就都认为master是真挂了，便将这个master的状态标记为<code>odown</code>(客观下线)</li>
<li>出现<code>odown</code>之后，所有sentinel就开会讨论谁去做故障转移这件事，sentinel发起投票，确定谁去做故障转移</li>
<li>挑选出来做故障处理的sentinel要依照下面的规则挑选备选master
<ol>
<li>在线的（排除掉下线的）</li>
<li>响应快的（排除掉响应慢的）</li>
<li>与原master沟通密切的（排除掉与原master断开时间久的）</li>
<li>有限原则
<ol>
<li>优先级</li>
<li>offset</li>
<li>runid</li>
</ol>
</li>
</ol>
</li>
<li>挑选出备选master之后，便向新的master发送<code>slaveof no one</code>的指令，将它升级到master，然后再向其他slave发送<code>slaveof 新masterIP 端口</code>，让其他slave全部指向新master</li>
</ol>
<p><strong>原master重新上线之后，会变成slave去连接现在的master</strong></p>
<h1 id="-36"><a class="markdownIt-Anchor" href="#-36"></a> <a href="#%E9%9B%86%E7%BE%A4" title="集群"></a>集群</h1>
<h2 id="-37"><a class="markdownIt-Anchor" href="#-37"></a> <a href="#%E7%8E%B0%E7%8A%B6%E9%97%AE%E9%A2%98" title="现状问题"></a>现状问题</h2>
<ul>
<li>redis提供的服务OPS可以达到10w/秒，当前业务OPS已经达到20w/秒</li>
<li>内存单机容量达到256G，当前业务需求内存容量1T</li>
</ul>
<p><strong>这时候就需要集群来解决上面的问题了</strong></p>
<h2 id="-38"><a class="markdownIt-Anchor" href="#-38"></a> <a href="#%E9%9B%86%E7%BE%A4%E6%9E%B6%E6%9E%84" title="集群架构"></a>集群架构</h2>
<p>集群就是使用网络将若干台计算机联通起来，并提供统一的管理方式，使其对外呈现单机的服务效果</p>
<p>集群作用：</p>
<ul>
<li>分散单台服务器的访问压力，实现负载均衡</li>
<li>分散单台服务器的存储压力，实现可扩展性</li>
<li>降低单台服务器宕机带来的业务灾难</li>
</ul>
<h2 id="-39"><a class="markdownIt-Anchor" href="#-39"></a> <a href="#%E9%9B%86%E7%BE%A4%E7%9A%84%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8" title="集群的数据存储"></a>集群的数据存储</h2>
<ul>
<li>通过算法设计，计算出key应该保存的位置
<ul>
<li>通过<code>CRC16(key)</code>得到一个数值，然后与<code>16384</code>取模，得到这个key最终应该保存的位置</li>
</ul>
</li>
<li>也就是说将所有的存储空间计划切割成16384份，每台主机保存一部分
<ul>
<li>每份代表的是一个存储空间，不是存储一个key的保存空间</li>
</ul>
</li>
<li>将key按照计算出的结果放到对应的存储空间</li>
<li>增强可扩展性</li>
</ul>
<h2 id="-40"><a class="markdownIt-Anchor" href="#-40"></a> <a href="#%E9%9B%86%E7%BE%A4%E7%9A%84%E5%86%85%E9%83%A8%E9%80%9A%E8%AE%AF%E8%AE%BE%E8%AE%A1" title="集群的内部通讯设计"></a>集群的内部通讯设计</h2>
<ul>
<li>各个数据库相互通信，保存各个库中槽的编号数据</li>
<li>一次命中，直接返回</li>
<li>一次未命中，告知具体位置</li>
</ul>
<h2 id="-41"><a class="markdownIt-Anchor" href="#-41"></a> <a href="#%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA" title="集群搭建"></a>集群搭建</h2>
<ul>
<li>搭建一个三主三从的集群</li>
</ul>
<p>cluster配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cluster-enabled <span class="built_in">yes</span>  </span><br><span class="line">cluster-config-file nodes-6379.conf  </span><br><span class="line">cluster-node-timeout 100000  </span><br></pre></td></tr></table></figure>
<p>大概的redis.conf配置如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">port 6379  </span><br><span class="line">daemonize <span class="built_in">yes</span>  </span><br><span class="line"><span class="built_in">dir</span> /redis-all-in/data  </span><br><span class="line">dbfilename dump-6379.rdb  </span><br><span class="line">rdbcompression <span class="built_in">yes</span>  </span><br><span class="line">rdbchecksum <span class="built_in">yes</span>  </span><br><span class="line">save 10 2  </span><br><span class="line">appendonly <span class="built_in">yes</span>  </span><br><span class="line">appendfsync always  </span><br><span class="line">appendfilename appendonly-6379.aof  </span><br><span class="line">databases 16  </span><br><span class="line">cluster-enabled <span class="built_in">yes</span>  </span><br><span class="line">cluster-config-file nodes-6379.conf  </span><br><span class="line">cluster-node-timeout 10000  </span><br></pre></td></tr></table></figure>
<p>启动命令（redis 5.0以后的启动方式）：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1  </span><br></pre></td></tr></table></figure>
<p>输入上面命令之后redis会自动分配槽位置与主从节点，确定没问题之后输入<code>yes</code>即可自动创建cluster集群</p>
<p><code>--cluster-replicas</code>表示1个master连1个slave</p>
<h2 id="-42"><a class="markdownIt-Anchor" href="#-42"></a> <a href="#Cluster%E8%8A%82%E7%82%B9%E6%93%8D%E4%BD%9C%E5%91%BD%E4%BB%A4" title="Cluster节点操作命令"></a>Cluster节点操作命令</h2>
<ul>
<li>查看集群节点信息：<code>cluster nodes</code></li>
<li>进入一个从节点，切换其主节点：<code>cluster replicate &lt;master-id&gt;</code></li>
<li>发现一个新节点，新增主节点：<code>cluster meet ip:port</code></li>
<li>忽略一个没有slot的节点：<code>cluster forget &lt;id&gt;</code></li>
<li>手动故障转移：<code>cluster failover</code></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">







<div class="license">
  <div class="license-title">Redis集群</div>
  <div class="license-link">
    <a href="https://blog.fantasyke.cn/posts/4509/">https://blog.fantasyke.cn/posts/4509/</a>
  </div>
  <div class="license-meta">
    <div class="license-meta-item">
      <div class="license-meta-title">本文作者</div>
      <div class="license-meta-text">
          Fantasy-ke
      </div>
    </div>
      <div class="license-meta-item">
        <div class="license-meta-title">发布于</div>
        <div class="license-meta-text">
          2023-06-01
        </div>
      </div>
      <div class="license-meta-item">
        <div class="license-meta-title">更新于</div>
        <div class="license-meta-text">
          2024-12-17
        </div>
      </div>
    <div class="license-meta-item">
      <div class="license-meta-title">许可协议</div>
      <div class="license-meta-text">
          禁止转载引用
      </div>
    </div>
  </div>
  <div class="license-statement">
      如需转载或引用本文，请先获得作者授权！
  </div>
</div>
          <div class="reward-container">
  <div>Buy me a coffee</div>
  <button>
    Donate
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Fantasy-ke WeChat Pay">
        <span>WeChat Pay</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>Welcome to my other publishing channels</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://github.com/fantasy-ke">
            <span class="icon">
              <i class="fab fa-github"></i>
            </span>

            <span class="label">GitHub</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Redis/" rel="tag"><i class="fa fa-tag"></i> Redis</a>
              <a href="/tags/NOSQL/" rel="tag"><i class="fa fa-tag"></i> NOSQL</a>
          </div>

        
  <div class="social-like a2a_kit a2a_kit_size_32 a2a_default_style">
    <a class="a2a_dd" target="_blank" rel="noopener" href="https://www.addtoany.com/share"></a>
      <a class="a2a_button_facebook"></a>
      <a class="a2a_button_twitter"></a>
      <a class="a2a_button_telegram"></a>
      <a class="a2a_button_wechat"></a>
  </div>

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/2961/" rel="prev" title="Redis企业级解决方案">
                  <i class="fa fa-angle-left"></i> Redis企业级解决方案
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/1297/" rel="next" title="Redis进阶">
                  Redis进阶 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2021 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-solid fa-t-rex"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Fantasy-ke</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>Word count total: </span>
    <span title="Word count total">72k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>Reading time total &asymp;</span>
    <span title="Reading time total">4:21</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div> 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  var now = new Date();
  function timer() {
    var grt= new Date("03/12/2022 13:14:21");//此处修改你的建站时间或者网站上线时间
    now.setTime(now.getTime()+250);
    days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
    hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
    if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
    seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
    var ages = `${dnum}天${hnum}小时${mnum}分${snum}秒`;
    div.innerHTML = `本站已安全运行 ${ages} 🐶`;
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".busuanzi-count");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>


<div class="footer-menu">
    <a style="color: chocolate;" href="/tags/">文章标签</a> · <a style="color: chocolate;"  href="/policy/">网站政策</a> · <a style="color: chocolate;"  href="/photos/">文章相册</a>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.umd.js" integrity="sha256-a+H7FYzJv6oU2hfsfDGM2Ohw/cR9v+hPfxHCLdmCrE8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>


  <script src="/js/third-party/addtoany.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"fantasy-ke","repo":"fantasy-ke.github.io","client_id":"Ov23li8eCFrhnzfiGx5T","client_secret":"ce23581c2059392ebd00afd08d5ba43f767fd1b1","admin_user":"fantasy-ke","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","labels":"gitalk","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"35ae1e804cef864ca81e1f97d5bfdde5"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>


<script src="/js/minigrid.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script data-pjax type="text/javascript">
var album = document.querySelector(".album");
if (album) {
  // 相册列表 JSON 数据
  var imgDataPath = album.getAttribute('json-src');
  // 照片存储路径
  var imgPath = album.getAttribute('photo-src');
  // 最多显示数量
  var imgMaxNum = 50;
  // 获取窗口大小以决定图片宽度
  var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var imageWidth;

  if (windowWidth < 768) {
    imageWidth = 145; // 移动端图片宽度
  } else {
    imageWidth = 235;
  }

  // 生成相册
  var linkDataPath = imgDataPath;
  var photo = {
    page: 1,
    offset: imgMaxNum,
    init: function () {
      var that = this;
      $.getJSON(linkDataPath, function (data) {
          that.render(that.page, data);
      });
    },
    render: function (page, data) {
      var begin = (page - 1) * this.offset;
      var end = page * this.offset;
      if (begin >= data.length) return;
      var imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
      for (var i = begin; i < end && i < data.length; i++) {
        imgNameWithPattern = data[i].path;
        imgName = data[i].name;
        imageSize = data[i].size;
        li += '<div class="card" style="width:' + imageWidth + 'px" >';
        li += '<div class="album-photo" style="height:' + imageWidth + 'px" >';
        li += '<a class="fancybox fancybox.image" href="' + imgPath + imgNameWithPattern + '" itemscope="" itemtype="http://schema.org/ImageObject" itemprop="url" data-fancybox="group" rel="group" data-caption="' + imgName + '" title="' +  imgName + '">';
        li += '<img data-src="' + imgPath + imgNameWithPattern + '" src="' + imgPath + imgNameWithPattern + '" alt="' +  imgName + '" data-loaded="true">';
        li += '</a>';
        li += '</div>';
        li += '</div>';
      }
      album.insertAdjacentHTML('beforeend', li);
      this.minigrid();
    },
    minigrid: function () {
      var grid = new Minigrid({
        container: '.album',
        item: '.card',
        gutter: 12
      });
      grid.mount();
      window.addEventListener('resize', function () {
        grid.mount();
      });
    }
  };
  photo.init();
}
</script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":170,"height":330},"mobile":{"show":true},"react":{"opacity":0.7},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
